#coding=utf-8
#import re
import os,sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'ssr.settings'
from ssr.seq.models import SeqFasta, SsrRecord

def parse_line(line):
  '''[gi, motif, n, start, end]'''
  data = line.strip().split('\t')
  gi = data[0].split('|')[1]
  motif = data[3]
  n = int(data[4])
  start = data[5]
  end = data[6]
  
  return [gi, motif, n, start, end]

if __name__ == '__main__':
  datafile = sys.argv[1]
  data = open(datafile).read().split('\n')
  data = [d for d in data if d]
  
  for line in data:
    r = parse_line(line)
  
    sr = SsrRecord()
    
    sr.seq = SeqFasta.objects.get(gi = r[0])
    sr.motif = r[1]
    sr.len_motif = len(r[1])
    sr.n = r[2]
    sr.start = r[3]
    sr.end = r[4]
   
    sr.save()
    
    print sr
     
